package com.bee.user.client.impl;

import com.bee.user.client.RoleResourceClient;
import com.bee.user.client.dto.ResourceDTO;
import com.bee.user.client.dto.RoleDTO;
import com.bee.user.common.domain.ResourceDO;
import com.bee.user.common.domain.RoleDO;
import com.bee.user.service.ResourceService;
import com.bee.user.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Damin.yang
 * @date 2019/08/23 14:17
 */
@RestController
public class RoleResourceClientImpl implements RoleResourceClient {

    @Autowired
    private ResourceService resourceService;
    @Autowired
    private RoleService roleService;

    @Override
    public List<ResourceDTO> queryByRoleId(Long roleId) {
        List<ResourceDO> list = resourceService.queryByRoleId(roleId);

        List<ResourceDTO> dtos = list.stream().map(resourceDO -> {
            ResourceDTO dto = new ResourceDTO();
            BeanUtils.copyProperties(resourceDO, dto);
            return dto;
        }).collect(Collectors.toList());

        return dtos;
    }

    @Override
    public List<RoleDTO> queryEnableRoles() {
        List<RoleDO> list = roleService.queryAllEnable();

        List<RoleDTO> dtos = list.stream().map(roleDO -> {
            RoleDTO dto = new RoleDTO();
            BeanUtils.copyProperties(roleDO, dto);
            return dto;
        }).collect(Collectors.toList());

        return dtos;
    }
}
